//=============================================================================
// LazyParser.NET - C# Expression Parser for .NET 2.0 
//
// Copyright (c) 2008 Philippe Leybaert
//
// Permission is hereby granted, free of charge, to any person obtaining a copy 
// of this software and associated documentation files (the "Software"), to deal 
// in the Software without restriction, including without limitation the rights 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
// copies of the Software, and to permit persons to whom the Software is 
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in 
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//=============================================================================

using System;
using System.Collections.Generic;

namespace Activa.LazyParser
{
    internal class Token
	{
        private readonly TokenDefinition _tokenDefinition;
		private readonly string          _text;

		internal int    NumTerms = 0;

    	private Token _alternate;

        internal Token(TokenDefinition tokenDefinition, string text)
		{
			_text = text;
			_tokenDefinition = tokenDefinition;

			switch (tokenDefinition.Type)
			{
				case TokenType.TernaryOperator: NumTerms = 3; break;
				case TokenType.UnaryOperator: NumTerms = 1; break;
				case TokenType.Operator: NumTerms = 2; break;
			}

			if (_tokenDefinition.Alternate != null)
				_alternate = new Token(_tokenDefinition.Alternate, text);
		}

		internal TokenType TokenType
		{
			get { return _tokenDefinition.Type; }
		}

    	internal OperatorAssociativity Associativity
    	{
			get { return _tokenDefinition.Associativity; }
    	}

		internal int Precedence
		{
			get { return _tokenDefinition.Precedence; }
		}

		internal string Text
		{
			get { return _text; }
		}

    	internal TokenEvaluator TokenEvaluator
    	{
			get { return _tokenDefinition.Evaluator; }
    	}

	    internal bool IsOperator
	    {
	        get { return (TokenType == TokenType.Operator) || (TokenType == TokenType.UnaryOperator); }
	    }

	    internal bool IsTerm
	    {
	        get { return (TokenType == TokenType.Term); }
	    }

	    internal bool IsUnary
	    {
	        get { return (TokenType == TokenType.UnaryOperator); }
	    }

	    internal bool IsFunction
	    {
	        get { return (TokenType == TokenType.FunctionCall); }
	    }

	    internal bool IsLeftParen
	    {
	        get { return (TokenType == TokenType.LeftParen); }
	    }

	    internal bool IsRightParen
	    {
	        get { return (TokenType == TokenType.RightParen); }
	    }

        internal bool IsArgumentSeparator
        {
            get { return TokenType == TokenType.ArgumentSeparator; }
        }

    	public bool IsPartial
    	{
			get { return _tokenDefinition.IsPartial; }
    	}

    	public Token Alternate
    	{
    		get { return _alternate; }
    		set { _alternate = value; }
    	}

    	public TokenDefinition Root
    	{
    		get { return _tokenDefinition.Root; }
    	}

    	public override string ToString()
		{
			return _text;
		}
	}
}
